package api

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/util/gconv"
	"github.com/zhang201702/zhang"
	"github.com/zhang201702/zhang/z"
	"github.com/zhang201702/zhang/zlog"
)

type ReportApi struct {
	BaseApi
}

func (api *ReportApi) Register(s *zhang.ServerGF) {
	s.BindHandler("/report/excel", api.excel)

	s.BindHandler("/report/school", api.school)
}

var cols = [...]string{
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
	"AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ",
	"BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ",
	"CA", "CB", "CC", "CD", "CE", "CF", "CG", "CH", "CI", "CJ", "CK", "CL", "CM", "CN", "CO", "CP", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ",
	"DA", "DB", "DC", "DD", "DE", "DF", "DG", "DH", "DI", "DJ", "DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "DW", "DX", "DY", "DZ",
}

func (api *ReportApi) excel(r *ghttp.Request) {
	var data = api.GetJsonMap(r)
	gj := gjson.New(data)
	excel := gj.GetArray("excel")
	name := gconv.String(data["name"])
	template := gj.GetString("template")

	xlFile, err := excelize.OpenFile(template)
	if err != nil {
		zlog.Error(err, "打开excel异常", template)
	}

	for _, v := range excel {

		sheet := v.(map[string]interface{})
		list := sheet["data"].([]interface{})
		starRow := gconv.Int(sheet["startRow"])
		startCol := gconv.Int(sheet["startCol"])
		sheetName := gconv.String(sheet["sheet"])
		for row, r1 := range list {
			switch r1.(type) {
			case []interface{}:
				{
					rd := r1.([]interface{})
					for col, v := range rd {
						vv := cols[col+startCol] + gconv.String(row+starRow)
						xlFile.SetCellValue(sheetName, vv, v)
					}
				}
			case map[string]interface{}:
				{
					m := r1.(map[string]interface{})
					rd := m["data"].([]interface{})
					for col, v := range rd {
						xlFile.SetCellValue(sheetName, cols[col+startCol]+gconv.String(row+starRow), v)
					}
					if objStyle, ok := m["style"]; ok {
						style, err2 := xlFile.NewStyle(gconv.String(objStyle))
						if err2 == nil {
							xlFile.SetCellStyle(sheetName, cols[startCol]+gconv.String(row+starRow), cols[startCol+len(rd)-1]+gconv.String(row+starRow), style)
						}
					}
					if objMerge, ok := m["merge"]; ok {
						merges := objMerge.([]interface{})
						for kk := range merges {
							m1 := merges[kk].(map[string]interface{})
							xlFile.MergeCell(sheetName, gconv.String(m1["a"]), gconv.String(m1["b"]))
						}
					}

				}
			}

		}

	}

	path := "result/" + name
	dir := gfile.Dir(path)
	if !gfile.Exists(dir) {
		gfile.Mkdir(dir)
	}
	if err != nil {
		zlog.Error(err, "保存excel异常")
		r.Response.Write(z.NewResult(nil, err))
	}
	err = xlFile.SaveAs(path)

	if path != "" {
		r.Response.WriteJson(z.Map{"data": path, "status": true})
	}
}

func (api *ReportApi) school(r *ghttp.Request) {
	var data = api.GetJsonMap(r)
	gj := gjson.New(data)
	list := gj.GetArray("list")
	name := gconv.String(data["name"])

	excelFileName := "html/report/4.3.xlsx"
	xlFile, err := excelize.OpenFile(excelFileName)
	if err != nil {
		zlog.Error(err, "打开excel异常", excelFileName)
	}

	for row, r := range list {

		rd := r.([]interface{})
		for col, v := range rd {
			xlFile.SetCellValue("3 中国赛成绩排名原始数据", cols[col]+gconv.String(row+3), v)
		}
		zlog.Log(row, len(rd))
	}

	path := "result/" + name

	dir := gfile.Dir(path)
	if !gfile.Exists(dir) {
		gfile.Mkdir(dir)
	}

	if err != nil {
		zlog.Error(err, "保存excel异常")
		r.Response.Write(z.NewResult(nil, err))
	}
	err = xlFile.SaveAs(path)

	if path != "" {
		r.Response.WriteJson(z.Map{"data": path, "status": true})
	}
}
